/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.museum.main;

import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.museum.utils.Utils;

public class DigLab {
	String[] users = new String[] { "chinesegxfi", };
	String[] uids = new String[] { "7122231616", };
	String[] cookies = new String[] {"MY:UserID=7122231616; MY:SNID=54f7c1030aa96958172bf758076ed1a5; MY:SESSION=4eb5f46206k898a0a7838bbc67d36f79; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F"};

	static Connection conn;
	int delay =20;// 2000 / users.length;
	DefaultHttpClient hc = new DefaultHttpClient();

	public void dig() throws Exception {
		HttpPost post = Utils.createPost();
		for (int i = 1; i <= 1500000;) {
			for (int j = 0; j < uids.length; j++) {
				try {
					Thread.sleep(delay);
					String url = "http://my.mowhere.com/interface/wa.php";
					post.setURI(new URI(url));
					post.setHeader("Cookie", cookies[j]);
					String x = String.valueOf(Utils.getRandomInt(1, 17));
					String y = String.valueOf(Utils.getRandomInt(1, 26));
					post.setEntity(Utils.genWaEntity(uids[j], x, y));
					String resjson = Utils.trans(hc.execute(post));
					JsonObject js = new JsonParser().parse(resjson)
							.getAsJsonObject();
					String err = js.get("err").getAsString();
					if ("体力不足".equals(err)) {
						sell(post, j);
						buy(post, j);
						use(post, j);
					} else if ("".equals(err)) {
						JsonObject data = js.getAsJsonObject("data");
						if (data.has("err")) {
							String dataerr = data.get("err").getAsString();
							if ("由于您的储物间已满，所以此宝物自动丢弃！<BR>".equals(dataerr)) {
								sell(post, j);
							}
						}
						if (data.has("treasuretext")) {
							String r = data.get("treasuretext").getAsString()
									.replaceAll("万金币。$", "0000金币。");
							System.out
									.println(i++ + "--" + users[j] + "==" + r);
							Pattern p = Pattern.compile("[\\d]+金币。$");
							Matcher m = p.matcher(r);
							String w = "0";
							if (m.find()) {
								w = m.group().replace("金币。", "");
							}

							String sql = "insert into dig_his(x,y,w,s) values("
									+ x + "," + y + "," + w + ",'" + r + "')";
							Statement stat = conn.createStatement();
							stat.execute(sql);
							// if (i % 60 == 0) {
							// sell(post, j);
							// }
							// if (i % 100 == 0) {
							// buy(post, j);
							// use(post, j);
							// }
						} else {
							System.out.println(i + "--" + users[j] + "==挖光了!");
							System.exit(0);
						}
					} else {
						System.out.println(i + "--" + users[j] + "==" + err);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void buy(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genBuyEntity(uids[uidx], "402"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(users[uidx] + "###" + r);
		}
	}

	public void use(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genUseEntity(uids[uidx], "402"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(users[uidx] + "###" + r);
		}
	}

	public void sell(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genSellEntity(uids[uidx], "all"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(users[uidx] + "###" + r);
		}
	}

	public static void main(String[] args) throws Exception {
		Class.forName("org.gjt.mm.mysql.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dig",
				"root", "sa");
		conn.setAutoCommit(true);
		DigLab r = new DigLab();
		r.dig();
		conn.close();
	}
}
